//
//  AppDelegate.m
//  FieldStory
//
//  Created by itte on 2017/6/21.
//  Copyright © 2017年 funmain. All rights reserved.
//

#import "AppDelegate.h"
#import "APPInfoHelper.h"
#import "WXApi.h"
#import <AlipaySDK/AlipaySDK.h>
#import <Bugly/Bugly.h>
#import "MainTabBarController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    // 初始化配置
    [self initConfig];
    // 第三方的配置
    [self initOtherConfig];
    // 初始化APPUI
    [self initAppDefaultUI];
    // 添加通知监听
    [self addObservers];
    
    // 显示场景
    [self loadMainScene];
    
    // 让LaunchScreen停留两秒
    [NSThread sleepForTimeInterval:1.0];
    [self.window makeKeyAndVisible];
    
    return YES;
}


- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}


- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

#pragma mark - 微信调用，重写父类方法
-(BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
    return [WXApi handleOpenURL:url delegate:[WXApiManager sharedManager]];
}

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
    if ([url.host isEqualToString:@"safepay"]) {
        // 支付跳转支付宝钱包进行支付，处理支付结果
        [[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
            NSLog(@"result = %@",resultDic);
        }];
        
        return YES;
    }
    else{
        return [WXApi handleOpenURL:url delegate:[WXApiManager sharedManager]];
    }
}

#pragma mark - 选择场景显示
// 主场景
-(void)loadMainScene
{
    MainTabBarController *maintabController = [[MainTabBarController alloc] init];
    self.window.rootViewController = maintabController;
}


#pragma mark - 初始化配置
// 初始化配置
-(void)initConfig
{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    BOOL bHadInitConfig = [userDefaults boolForKey:@"bHadInitConfig"];
    if (bHadInitConfig) {
        return ;
    }
    NSDate *date = [NSDate date];
    NSTimeInterval timeInterval = [date timeIntervalSince1970];
    [userDefaults setObject:[NSNumber numberWithBool:YES] forKey:@"bHadInitConfig"];
    [userDefaults setObject:[NSString stringWithFormat:@"%f",timeInterval] forKey:@"initTime"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}
// 初始化第三方配置
-(void)initOtherConfig
{
    // JSPath配置
    // [self setupJSPath];
    // Bugly配置
    [Bugly startWithAppId:KBuglyAppID];
    
    // 微信配置
    [WXApi registerApp:KWxAppID withDescription:@"田野故事"];
}


#pragma mark - 应用默认初始化
- (void)initAppDefaultUI
{
    //将状态栏字体改为白色
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
    //改变Navibar的默认颜色
     // [[UINavigationBar appearance] setBarTintColor:[UIColor blackColor]];
    //设置字体为白色
    [[UINavigationBar appearance] setTintColor:[UIColor blackColor]];
    //设置Title为白色,Title大小为18
    [[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor blackColor], NSFontAttributeName:[UIFont boldSystemFontOfSize:18]}];
    [[UITabBar appearance] setBarTintColor:[UIColor colorwithHexString:@"#ffffff"]];
    [[UITabBar appearance] setBackgroundImage:[UIImage imageNamed:@"tabbar_bg"]];
}


#pragma mark - 监听NSNotification
- (void)addObservers
{
    // 监听登录通知
}

@end
